<?php
session_start();
    
    // Created By : Julian Ponce
    // B.S. Computer Science - De La Salle Canlubang
    // December 4, 2011
    // IN PARAMETERS : 
    // 
    // This AJAX Request creates new record of security group and its corresponding permissions.

    include('../includes/siteConfig.php');
    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // VARIABLES VIA POST
    $securityGroupId = $_POST['securityGroupId'];
    $securityGroupName = $_POST['securityGroupName'];
    $permissionCount = $_POST['permissionCount'];
    $permissionString = $_POST['permissionString'];
    
    // SAVE SECURITY GROUP INFO
    $query = 
        "
            INSERT INTO SecurityGroup (securityGroupId, securityGroupName)
            VALUES ('$securityGroupId', '$securityGroupName')
        ";
    
    mysql_query($query);
    
    
    // SAVE PERMISSIONS OF SECURITY GROUP
    $secGrpPermissions = explode(",", $permissionString);

        for ($x=0; $x< $permissionCount;$x++) {
            $query = 
                "
                    INSERT INTO SecurityGroup_Permission (securityGroupId, permissionId)
                    VALUES ('$securityGroupId', '$secGrpPermissions[$x]')
                ";
            mysql_query($query);
        }
    
    mysql_close($connect);
    
    echo json_encode(1);
?>
